package com.jxaummd.mushroom.main.user.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.jxaummd.mushroom.R;
import com.jxaummd.mushroom.R2;
import com.jxaummd.mushroom.core.base.fragment.BaseBottomFragment;
import com.jxaummd.mushroom.core.base.fragment.BaseFragment;
import com.jxaummd.mushroom.main.user.item.ListItem;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.davidea.flexibleadapter.common.FlexibleItemDecoration;

public class ListFragment extends BaseFragment {

    @BindView(R2.id.rv_common_list)
    RecyclerView mRvList = null;

    @BindView(R2.id.tv_title)
    TextView mTvTitle = null;

    String mTitle = "";

    List<ListBean> lists = null;

    public static ListFragment newInstance(String title, List<ListBean> lists) {
        Bundle args = new Bundle();
        ListFragment fragment = new ListFragment();
        fragment.setArguments(args);
        fragment.mTitle = title;
        fragment.lists = lists;
        return fragment;
    }


    /**
     * 设置layout
     *
     * @return
     */
    @Override
    public Object setLayout() {
        return R.layout.common_list_fragment_layout;
    }

    /**
     * 视图逻辑绑定
     *
     * @param savedInstanceState
     * @param rootView
     */
    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mTvTitle.setText(mTitle);

        List<ListItem> listItems = new ArrayList<>();
        for (ListBean bean:lists){
            listItems.add(new ListItem(bean));
        }

        FlexibleAdapter adapter = new FlexibleAdapter(listItems,this);
        FlexibleItemDecoration mItemDecoration = new FlexibleItemDecoration(getActivity())
                .withOffset(3) // This helps when top items are removed!!
                .withDivider(R.drawable.ic_line, R.layout.user_item_single_text)
                .withBottomEdge(true);
        mRvList.setAdapter(adapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mRvList.addItemDecoration(mItemDecoration);
    }

   public static class ListBean {
        String title;
        String describe;
        View.OnClickListener listener;

        /**
         * 有参构造函数
         *
         * @param title
         * @param describe
         * @param listener
         */
        public ListBean(String title, String describe, View.OnClickListener listener) {
            this.title = title;
            this.describe = describe;
            this.listener = listener;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getDescribe() {
            return describe;
        }

        public void setDescribe(String describe) {
            this.describe = describe;
        }

        public View.OnClickListener getListener() {
            return listener;
        }

        public void setListener(View.OnClickListener listener) {
            this.listener = listener;
        }
    }
}
